from django.db import models
from pms.common.utils import cn_datetime


class Mail(models.Model):
    id = models.BigAutoField(primary_key=True)
    title = models.CharField(max_length=200)
    uid = models.ForeignKey('user.User', to_field='id', on_delete=models.SET_NULL, null=True, db_column='uid')
    content = models.TextField(default='')
    date_time = models.DateTimeField(default=cn_datetime)
    did = models.ForeignKey('department.Department', to_field='id', on_delete=models.SET_NULL, null=True,
                            default=None, db_column='did')
    pid = models.ForeignKey('project.Project', to_field='id', on_delete=models.SET_NULL, null=True, default=None,
                            db_column='pid')

    class Meta:
        db_table = 'mail'
        ordering = ['id']


class Notice(models.Model):
    id = models.BigAutoField(primary_key=True)
    title = models.CharField(max_length=200)
    uid = models.ForeignKey('user.User', to_field='id', on_delete=models.SET_NULL, null=True, db_column='uid')
    content = models.TextField(default='')
    date_time = models.DateTimeField(default=cn_datetime)
    did = models.ForeignKey('department.Department', to_field='id', on_delete=models.CASCADE, null=True,
                            default=None, db_column='did')
    pid = models.ForeignKey('project.Project', to_field='id', on_delete=models.CASCADE, null=True, default=None,
                            db_column='pid')

    class Meta:
        db_table = 'notice'
        ordering = ['id']


class MailReceive(models.Model):
    mid = models.ForeignKey('Mail', to_field='id', on_delete=models.CASCADE, db_column='mid')
    uid = models.ForeignKey('user.User', to_field='id', on_delete=models.CASCADE, db_column='uid')
    is_delete = models.BooleanField(default=False)

    class Meta:
        db_table = 'mail_receive'
        constraints = [
            models.UniqueConstraint(fields=['mid', 'uid'], name='mail_receive_id')
        ]
